
using System.Collections.Generic;
using M0;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public static partial class M
{
    public const string EventKey_切换场景 = "EventKey_切换场景";
    public const string EventKey_保存数据 = "EventKey_保存数据";
    public const string EventKey_游戏开始 = "EventKey_游戏开始";
    
    public static async void 切换场景(string sceneName)
    {
        await SceneManager.LoadSceneAsync(sceneName);
        Event.DoEvent(EventKey_切换场景,sceneName);
    }


    public static bool NullOrEmpty(this string s)
    {
        return string.IsNullOrEmpty(s);
    }



    public static void 保存缓存(string key,string content)
    {
        Boot.Instance.Get小游戏.保存数据(key,content);
    }

    public static T Get缓存<T>(string key) where T:new()
    {
        int a=0;
        string content="";
        Boot.Instance.Get小游戏.Get本地缓存<string>(key,ref a,ref content);
        if (string.IsNullOrEmpty(content))
        {
            return new T();
        }
        T res = JsonConvert.DeserializeObject<T>(content);
        return res;
    }
    // public static bool TryAdd<T, M>(this Dictionary<T, M> dict, T key, M value)
    // {
    //     if(dict)
    // }


    /// <summary>
    /// list 深拷贝
    /// </summary>
    /// <param name="res"></param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static List<T> CopyList<T>(this List<T> res)
    {
        List<T> r = new List<T>();
        foreach (var VARIABLE in res)
        {
            r.Add(VARIABLE);
        }
        return r;
    }

    public static JsonV3 ToV3(this Vector3 v3)
    {
        JsonV3 v = new JsonV3() {x = v3.x, y = v3.y, z = v3.z};
        return v;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public static bool 点到Ui(int 参考=0)
    {
        var eventDataCurrentPosition = new PointerEventData(EventSystem.current);
        eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
        
        GraphicRaycaster graphicRaycaster = Object.FindObjectOfType<GraphicRaycaster>();
        List<RaycastResult> results = new List<RaycastResult>();
        graphicRaycaster.Raycast(eventDataCurrentPosition, results); // 执行射线投射，获取结果列表
        
        return results.Count > 参考; // 如果有结果，则表示点击在UI元素上
    }

    // private static RaycastHit[] hitResults = new RaycastHit[];
    private static RaycastHit[] hitResults = new RaycastHit[10];
    public static T 射线检测<T>(Ray ray)
    {
        // RaycastHit[] hitResults = new RaycastHit[10];
        var numHits = Physics.RaycastNonAlloc(ray, hitResults);

        T res = default(T);
        if (numHits > 0)
        {
            res = hitResults[0].collider.gameObject.GetComponent<T>();
            // if (res != null)
            // {
            //     Debug.Log(组件);
            // }
        }

        return res;
    }
    
    /// <summary>
    /// 单例-----------------------------------
    /// </summary>
    public static MyEvent Event
    {
        get
        {
            return MyEvent.Instance;
        }
    }
    
    public static Resource加载 Resource
    {
        get
        {
            return Resource加载.Instance;
        }
    }
    
    public static Mono缓存池 缓存{get{return Mono缓存池.Instance;}}
}
